/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.view;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import org.cw.bean.Establishment;
import org.cw.bean.Reviews;
import org.cw.bo.ReviewBO;

/**
 *
 * @author SVN - Team
 */
public class FromViewEstablistment extends Form implements CommandListener {

    private static final String TITLE = "Details Estlablishment";
    private MReviewer curDisplay;
    private Establishment est;
    private StringItem txtID, lbName, lbLocation, lbTypeOfEst, lbTypeOfFood;
    private Command cmdBack, cmdNewReview, cmdViewBestReview, cmdViewDetailReview, cmdDelReview;
    private FromAddEditReview addReview;
    private FromListEstablishments listEsts;
    private ChoiceGroup lReviews;
    private Image iconDetail;
    private StringItem titleViewDetails, titleListReview;
    private Command cmdAllReview, cmdEditReview, cmdAcept, cmdCancel;
    private FromAddEditReview frmEditReview;
    private Alert msg;
    private ReviewBO reBO = ReviewBO.getInstance();

    public FromViewEstablistment(MReviewer curDisplay) {
        super(TITLE);
        this.curDisplay = curDisplay;
    }

    public void setEstablishment(Establishment est) {
        this.est = est;
        try {
            iconDetail = Image.createImage("/detail.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        append(iconDetail);
        titleViewDetails = new StringItem("", "View Detail Establishment");
        titleViewDetails.setLayout(StringItem.LAYOUT_2);
        append(titleViewDetails);

      //  StringItem lbid = new StringItem("", "ID");
      //lbid.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
      //  append(lbid);
        txtID = new StringItem("ID", est.getId());
        txtID.setLayout(StringItem.LAYOUT_2);
        append(txtID);

        lbName = new StringItem("Name: ", est.getName());
        lbTypeOfEst = new StringItem("Type Of Establishment: ", est.getType());
        lbTypeOfFood = new StringItem("Type Of Food: ", est.getTypeOfServices());
        lbLocation = new StringItem("Location: ", est.getLocation());
        titleListReview = new StringItem("List Review of Establishment", "");
        lReviews = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE);
        cmdBack = new Command("Back", Command.BACK, 0);
        cmdNewReview = new Command("add Review", Command.OK, 1);
        cmdViewBestReview = new Command("best Review", Command.OK, 2);
        cmdViewDetailReview = new Command("Details", Command.OK, 3);
        cmdAllReview = new Command("All Review", Command.OK, 4);


        append(lbName);
        append(lbTypeOfEst);
        append(lbTypeOfFood);
        append(lbLocation);
        append(titleListReview);
        append(lReviews);
        addCommand(cmdBack);
        addCommand(cmdNewReview);
        setCommandListener(this);
        if (est.getListReview().size() > 0) {
            cmdEditReview = new Command("Edit", Command.OK, 5);
            cmdDelReview = new Command("Remove", Command.OK, 6);
            addCommand(cmdEditReview);
            addCommand(cmdViewBestReview);
            addCommand(cmdDelReview);
            addCommand(cmdAllReview);
        }
        initDataList();
    }

    private void initDataList() {
        lReviews.deleteAll();
        Vector lRe = est.getListReview();
        if (lRe.size() <= 0) {
            lReviews.setLabel("Do not have a record Review.");
            removeCommand(cmdEditReview);
            removeCommand(cmdViewBestReview);
            removeCommand(cmdDelReview);
            removeCommand(cmdAllReview);
        } else {
            lReviews.setLabel("There are " + lRe.size() + " Records");
            for (int i = 0; i < lRe.size(); i++) {
                Reviews r = (Reviews) lRe.elementAt(i);
                lReviews.append("- Date:" + r.getDate() + "\n"
                        + "- Type:" + r.getTypeOfMeal() + "\n"
                        + "- Cost:" + r.getCost() + "$\n"
                        + "- OverRall rate:" + getStarByPoint(r.getOverallRating()) + "(" + r.getOverallRating() + "/5)" + "\n"
                        + "- Rating for Services:" + getStarByPoint(r.getRateOfServices()) + "(" + r.getRateOfServices() + "/5)" + "\n"
                        + "- Rating for atmosphere:" + getStarByPoint(r.getRateOfAtmosphere()) + "(" + r.getRateOfAtmosphere() + "/5)" + "\n"
                        + "- Rating for food:" + getStarByPoint(r.getRateOfFood()) + "(" + r.getRateOfFood() + "/5)" + "\n"
                        + "- Comment:" + r.getComment() + "\n", null);
            }
        }
    }

    private String getStarByPoint(int pRate) {
        String strStar = "";
        for (int i = 0; i < pRate; i++) {
            strStar += "★";
        }
        return strStar;
    }
    private String myData = "";

    public void commandAction(Command c, Displayable d) {
        if (c == cmdBack) {
            listEsts = new FromListEstablishments(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(listEsts);
        } else if (c == cmdNewReview) {
            addReview = new FromAddEditReview(curDisplay);
            addReview.setEst(est, FromAddEditReview.ADD);
            Display.getDisplay(curDisplay).setCurrent(addReview);
        } else if (c == cmdViewBestReview) {
            setReviewBest();
        } else if (c == cmdViewDetailReview) {
        } else if (c == cmdAllReview) {
            initDataList();
        } else if (c == cmdEditReview) {
            frmEditReview = new FromAddEditReview(curDisplay);
            frmEditReview.setEst(est, FromAddEditReview.EDIT);
            Reviews re = reBO.getReviewByData(est, lReviews.getString(lReviews.getSelectedIndex()));
            frmEditReview.setDataFrom(re);
            Display.getDisplay(curDisplay).setCurrent(frmEditReview);
        } else if (c == cmdDelReview) {
            myData = lReviews.getString(lReviews.getSelectedIndex());
            msg = new Alert("Message", "Do you want delete review?", null, AlertType.CONFIRMATION);
            msg.setTimeout(Alert.FOREVER);
            cmdAcept = new Command("OK", Command.OK, 1);
            cmdCancel = new Command("Cancel", Command.CANCEL, 0);
            msg.addCommand(cmdAcept);
            msg.addCommand(cmdCancel);
            msg.setCommandListener(this);
            Display.getDisplay(curDisplay).setCurrent(msg, this);
        } else if (c == cmdAcept) {
            removeReview(myData);
        } else if (c == cmdCancel) {
            Display.getDisplay(curDisplay).setCurrent(this);
        }
    }

    public void removeReview(String strData) {
        System.out.println("Data:" + strData);
        Reviews re = reBO.getReviewByData(est, strData);
        boolean flag = reBO.delete(re);
        System.out.println("are you ok? " + flag);
        if (flag) {
            for (int i = 0; i < est.getListReview().size(); i++) {
                Reviews rDel = (Reviews) est.getListReview().elementAt(i);
                if (re.toString().equals(rDel.toString())) {
                    est.getListReview().removeElementAt(i);
                    break;
                }
            }
            for (int i = 0; i < MReviewer.dataStore.size(); i++) {
                Establishment temp = (Establishment) MReviewer.dataStore.elementAt(i);
                if (est.getId().equals(temp.getId())) {
                    MReviewer.dataStore.setElementAt(est, i);
                    break;
                }
            }
            msg = new Alert("Message", "You have deleted successfullĂ", null, AlertType.ALARM);
            Display.getDisplay(curDisplay).setCurrent(msg,this);
        } else {
             msg = new Alert("Error", "you don't delete Review, Please try again!", null, AlertType.ERROR);
            Display.getDisplay(curDisplay).setCurrent(msg,this);
        }
       // Display.getDisplay(curDisplay).setCurrent(this);
        initDataList();
    }

    private void setReviewBest() {
        lReviews.deleteAll();
        Vector vReview = est.getListReview();
        Vector lBestReview = reBO.getBestReview(est);
        for (int i = 0; i < lBestReview.size(); i++) {
            Reviews r = (Reviews) lBestReview.elementAt(i);
            lReviews.append("- Date:" + r.getDate() + "\n"
                    + "- Type:" + r.getTypeOfMeal() + "\n"
                    + "- Cost:" + r.getCost() + "$\n"
                    + "- OverRall rate:" + getStarByPoint(r.getOverallRating()) + "(" + r.getOverallRating() + "/5)" + "\n"
                    + "- Rating for Services:" + getStarByPoint(r.getRateOfServices()) + "(" + r.getRateOfServices() + "/5)" + "\n"
                    + "- Rating for atmosphere:" + getStarByPoint(r.getRateOfAtmosphere()) + "(" + r.getRateOfAtmosphere() + "/5)" + "\n"
                    + "- Rating for food:" + getStarByPoint(r.getRateOfFood()) + "(" + r.getRateOfFood() + "/5)" + "\n"
                    + "- Comment:" + r.getComment() + "\n", null);

        }
        lReviews.setLabel("There are " + lBestReview.size() + " Reviews best in Establishment");
    }
}
